#include "av.h"

extern
int lua_new_frame(lua_State * L) {
	lua_push_frame(L, avcodec_alloc_frame());
	return 1;
}

static
void lua_av_destroy_picture(void * pict) {
	avpicture_free(pict);
	av_free(pict);
}

static
int lua_av_alloc_picture(lua_State * L) {
	AVFrame * frame;
	lua_av_object * object = lua_check_frame(L, 1, &frame);
	enum PixelFormat pf = lua_check_pixel_format(L, 2);
	int w = luaL_checkinteger(L, 3);
	int h = luaL_checkinteger(L, 4);
	avpicture_alloc((AVPicture *)frame, pf, w, h);
	object->destroy = lua_av_destroy_picture;
	return 0;
}

static
const struct luaL_reg av_frame_reg[] = {
	{"alloc_picture", lua_av_alloc_picture},
	{NULL, NULL},
};

DEFINE_TYPE_STRUCT(frame, AVFrame,
	LUA_OBJECT_FRAME_ID, av_free, av_frame_reg)

DEFINE_INDEX_HEADER(frame, AVFrame)
	HANDLE_INDEX_ENTRY(key_frame, lua_pushboolean);
	HANDLE_INDEX_ENTRY(interlaced_frame, lua_pushboolean)
	HANDLE_INDEX_ENTRY(top_field_first, lua_pushboolean)
	HANDLE_INDEX_ENTRY(coded_picture_number, lua_pushinteger)
	HANDLE_INDEX_ENTRY(display_picture_number,lua_pushinteger)
DEFINE_INDEX_TRAILER()

DEFINE_NEWINDEX_HEADER(frame, AVFrame)
DEFINE_NEWINDEX_TRAILER()

DEFINE_OBJECT_FUNCTIONS(frame, AVFrame)
